package com.jihui.user.controller.login;

import com.jihui.enterprise.model.EnterpriseManager;
import com.jihui.enterprise.model.JihuiEnterprise;
import com.jihui.feign.EnterpriseManagerStart;
import com.jihui.user.model.JihuiUser;
import com.jihui.user.model.UserManager;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.mvnsearch.boot.oss.ByteArrayDataSource;
import org.mvnsearch.boot.oss.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;


/**
 * Created by wangjin on 2017/5/19.
 */
@RestController
public class TestController{
    @Autowired
    private EnterpriseManager enterpriseManager;
    @Autowired
    private UserManager userManager;
//    @Autowired
//   private UserManagerStart userManagerStart;
    @Resource
    private FileStorageService fileStorageService;



    @Autowired
    private EnterpriseManagerStart enterpriseManagerStart;
   // @Autowired
    //private UserService userService;

    @RequestMapping(value = "/save", method = RequestMethod.GET)
    @ResponseBody
    public void add() {
        JihuiEnterprise jihuiEnterprise = new JihuiEnterprise();
        jihuiEnterprise.setUserId(131L);
        jihuiEnterprise.setName("adsffffasdf");
        enterpriseManager.save(jihuiEnterprise);
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ResponseBody
    public JihuiUser findById() {
        return userManager.findById(1L);
    }
    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public String upload(ModelMap model) {
        try {

            //InputStream in = new InputStream());
            File file = new File("/Users/wangjin/Desktop/job/测试批量打回提现.xls");
            //获得文件
            FileInputStream in = new FileInputStream(file);
            //String fileName = file.getOriginalFilename();
            byte[] content = IOUtils.toByteArray(in);
            ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(content, "xls");
            byteArrayDataSource.setName(file.getName());
            String path = fileStorageService.saveToDirectory("financing/excel" , byteArrayDataSource);
            System.out.println("path:"+path);
        } catch (IOException e) {
            return StringUtils.EMPTY;
        }
         return "/test";
    }
    @RequestMapping("/helloHtml")
    public String helloHtml(Map<String,Object> map){
//        JihuiEnterprise jihuiEnterprise = new JihuiEnterprise();
//        jihuiEnterprise.setName("asdf");
//        jihuiEnterprise.setUserId(55L);
        //enterpriseManagerStart.save(jihuiEnterprise);
        map.put("user" , userManager.findById(1L));
        map.put("hello","from TemplateController.helloHtml");
        return "/test";
    }
    @RequestMapping("/tests")
    public String tests(Map<String,Object> map){
        JihuiEnterprise jihuiEnterprise = new JihuiEnterprise();
        jihuiEnterprise.setName("asdfasdf");
        jihuiEnterprise.setUserId(333L);
        enterpriseManager.save(jihuiEnterprise);
        return "/test";
    }
}
